package com.luotao.test.hbase.web;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-28
 * Time: 17:59
 */
public class InsertServlet extends HttpServlet {

    private HTable table;

    private String colfam = "data";

    private String colfam1 = "1";

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            System.out.println("init servlet");
            Configuration conf = HBaseConfiguration.create();
            HTableDescriptor htd = new HTableDescriptor("test2");
            byte[] tablename = htd.getName();
            this.table = new HTable(conf, tablename);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String str_uuid = UUID.randomUUID().toString();
        Put p = new Put(str_uuid.getBytes());
        p.add(colfam.getBytes(),
                colfam1.getBytes(),
                "hello456".getBytes());
        table.put(p);
        System.out.println("insert success");
    }
}
